package com.d404.server;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import com.alibaba.fastjson.JSON;

import im.notify.server.Notify;
import manager.DefaultSessionManager;
import manager.MessageManager;
import manager.PersistentManager;

/**
 * 查询此闸门的负责人的位置 Servlet implementation class GetUserLocation
 */
// @WebServlet("/GetUserLocation")
public class GetUserLocation extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public GetUserLocation() {
		super();
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// response.getWriter().append("Served at:
		// ").append(request.getContextPath());

		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		
		String data = request.getParameter("data");
		Upload upload = JSON.parseObject(data,Upload.class);
		Download download = new Download();

		SessionFactory sf = PersistentManager.sharemanager().getSessionFactory();
		Session session = sf.getCurrentSession();
		Transaction tx = session.beginTransaction();
		try {

			Query q = session.createQuery("select u.account from IrrBAWaterGate gate,User u where u.realName=gate.engManCd and gate.watergateCode = ?");
			q.setParameter(0, upload.getGateId());
			
			List results = q.list();
			String account = "";
			if (!results.isEmpty()) {
				account = (String) results.get(0);;
			}
			
			Notify.GetLocation getLocation = new Notify.GetLocation();
			getLocation.setAccount(upload.getAsker());
			getLocation.setGateId(upload.getGateId());
			MessageManager.sendSimpleMessage(account, JSON.toJSONString(getLocation));
			
			tx.commit();
			
			if (account != null && DefaultSessionManager.share().containsCIMSession(account)) {
				download.setStatus(Download.SUCCESS);
			}
			else
			{
				download.setStatus(Download.NOTONLINE);
			}
		} catch (Exception e) {
			download.setStatus(Download.FAILED);
			tx.rollback();
			e.printStackTrace();
		} finally {

		}

		System.out.println(JSON.toJSONString(download));
		response.getWriter().append(JSON.toJSONString(download));
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

	// 上行数据
	public static class Upload {
		String asker; // 请求人
		String gateId; // 闸门编号

		public String getAsker() {
			return asker;
		}

		public void setAsker(String asker) {
			this.asker = asker;
		}

		public String getGateId() {
			return gateId;
		}

		public void setGateId(String gateId) {
			this.gateId = gateId;
		}

	}

	// 下行数据
	public static class Download {
		public static int SUCCESS = 1;
		public static int FAILED = 2;
		public static int NOTONLINE = 3;
		int status;

		public int getStatus() {
			return status;
		}

		public void setStatus(int status) {
			this.status = status;
		}
		
	}
}
